package com.bytedance.improvingroad.widget

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.bytedance.improvingroad.R
import com.bytedance.improvingroad.utils.Utils

/**
 * Created by chenxiong
 * date 1/30/22
 */
class MultiTouchView @JvmOverloads constructor(
    context: Context,
    attr: AttributeSet? = null,
    style: Int = 0
) : View(context, attr, style) {

    private val IMAGE_WIDTH: Float = Utils.dp2px(300).toFloat()
    private var bitmap: Bitmap? = null
    private var paint: Paint? = null

    // 手指滑动偏移值
    private var offsetX = 0f
    private var offsetY = 0f

    // 按下时的x,y坐标
    private var downX = 0f
    private var downY = 0f

    // 上一次的偏移值
    private var lastOffsetX = 0f
    private var lastOffsetY = 0f

    private var currentId = 0

    init {
        init()
    }

    private fun init() {
        val option = BitmapFactory.Options()
        option.inJustDecodeBounds = true
        BitmapFactory.decodeResource(resources, R.drawable.pic1, option)
        option.inJustDecodeBounds = false
        option.inDensity = option.outWidth
        option.inTargetDensity = Utils.dp2px(300)
        bitmap = BitmapFactory.decodeResource(resources, R.drawable.pic1, option)
        paint = Paint(Paint.ANTI_ALIAS_FLAG)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawBitmap(bitmap!!, offsetX, offsetY, paint)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when(event.actionMasked) {
            MotionEvent.ACTION_DOWN -> {
                downX = event.x
                downY = event.y
                lastOffsetX = offsetX
                lastOffsetY = offsetY
                currentId = 0
            }
            MotionEvent.ACTION_MOVE -> {
                val index = event.findPointerIndex(currentId)
                offsetX = lastOffsetX + event.getX(index) - downX
                offsetY = lastOffsetY + event.getY(index) - downY
                invalidate()
            }
            MotionEvent.ACTION_POINTER_DOWN -> {
                val pointerIndex = event.actionIndex
                currentId = event.getPointerId(pointerIndex)
                downX = event.getX(pointerIndex)
                downY = event.getY(pointerIndex)
                lastOffsetX = offsetX
                lastOffsetY = offsetY
            }
            MotionEvent.ACTION_POINTER_UP -> {
                var upIndex = event.actionIndex
                val id = event.getPointerId(upIndex)
                if (id == currentId) {
                    if (upIndex == event.pointerCount - 1) {
                        upIndex = event.pointerCount - 2
                    } else {
                        upIndex++
                    }
                    currentId = event.getPointerId(upIndex)
                    downX = event.getX(upIndex)
                    downY = event.getY(upIndex)
                    lastOffsetX = offsetX
                    lastOffsetY = offsetY
                }
            }
        }
        return true
    }
}